package com.itcast.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itcast.reggie.commen.CustomException;
import com.itcast.reggie.mapper.FoodClassDao;
import com.itcast.reggie.pojo.Category;
import com.itcast.reggie.pojo.Dish;
import com.itcast.reggie.pojo.Setmeal;
import com.itcast.reggie.service.DishService;
import com.itcast.reggie.service.FoodClassService;
import com.itcast.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FoodClassServiceImpl extends ServiceImpl<FoodClassDao, Category> implements FoodClassService{
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    //根据id删除,删除前会判断有没有关联
    @Override
    public void remove(Long id) {
        //判断菜品是否与菜品关联
        QueryWrapper<Dish> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",id);
        int count1 = dishService.count(wrapper);
        if(count1 > 0){
            throw new CustomException("当前分类下关联了菜品,不能删除");
        }
        //判断菜品是否与套餐关联
        QueryWrapper<Setmeal> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("category_id",id);
        int count2 = setmealService.count(wrapper2);
        if(count2 > 0){
            throw new CustomException("当前分类下关联了套餐,不能删除");
        }
        //不关联,则删除
        super.removeById(id);
    }
}
